package org.jgs1904.controller.skip;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: 默苍璃
 * @date: 2023-05-13 10:07
 * <p>
 * ServletAPI , 不需要视图解析器 .
 */
@Controller
public class ServletAPIController {


    /**
     * 通过HttpServletResponse进行输出
     *
     * @param req
     * @param rsp
     * @throws IOException
     */
    @RequestMapping("/result/t1")
    public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        System.out.println("ServletAPIController");
        rsp.getWriter().println("Hello,Spring BY servlet API");
    }


    /**
     * 通过HttpServletResponse实现重定向
     *
     * @param req
     * @param rsp
     * @throws IOException
     */
    @RequestMapping("/result/t2")
    public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        rsp.sendRedirect("/index.jsp");
    }


    /**
     * 通过HttpServletResponse实现转发
     *
     * @param req
     * @param rsp
     * @throws Exception
     */
    @RequestMapping("/result/t3")
    public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
        //转发
        req.setAttribute("msg", "/result/t3");
        req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req, rsp);
    }


}
